package com.project_doctor.controller;

import com.project_doctor.pojo.Conversation;
import com.project_doctor.pojo.Dto.ConversationRequest;
import com.project_doctor.service.ConversationService;
import com.project_doctor.service.DoctorService;
import com.project_doctor.service.MessageService;
import com.project_doctor.util.JwtUtil;
import io.minio.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("conversation")
public class ConversationController {
    @Autowired
    private ConversationService conversationService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    MessageService messageService;

    @Autowired
    JwtUtil jwtUtil;
    // 获取用户所有对话
    @PostMapping("/getAllConversations/{id}")
    public ResponseEntity<?> getAllConversations(@PathVariable Long id) {
        List<Conversation> conversations = conversationService.getAllConversations(id);
        return ResponseEntity.ok(conversations);
    }

    // 获取单个对话详情
    @GetMapping("/getConversationById/{id}")
    public ResponseEntity<?> getConversationById(@PathVariable Long id) {
        Conversation conversation = conversationService.getConversationById(id);
        conversation.setStarter(doctorService.getUserById(conversation.getStarterId()));
        conversation.setParticipant(doctorService.getUserById(conversation.getParticipantId()));
        conversation.setMessages(messageService.getMessagesByConversationId(conversation.getId()));

        log.info("查询对话Id是：         "+conversation.getId().toString());
        return ResponseEntity.ok(conversation);
    }

    // 创建新对话
    @PostMapping("/createConversation")
    public ResponseEntity<?> createConversation(@RequestBody ConversationRequest request) {
        Long participantId = request.getParticipantId();
        Long starterId = request.getStarterId();
        Conversation conversation = conversationService.getOrCreateConversation(starterId, participantId);
        return ResponseEntity.ok(conversation);
    }

    // 标记已读
    @PutMapping("/markAsRead/{conversationId}/{id}")
    public ResponseEntity<?> markAsRead(
            @PathVariable Long conversationId,
            @PathVariable Long id) {
        conversationService.markAsRead(conversationId,id);
        return ResponseEntity.ok("标记已读成功");
    }

    @DeleteMapping("/deleteConversation/{id}")
    public ResponseEntity<?> deleteConversation(@PathVariable Long id) {
        conversationService.removeById(id);
        return ResponseEntity.ok("删除成功");
    }


}
